// pages/business/user-manage/user-manage.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNumber: 1, // 当前页码
    hasNextPage: true, // 是否有下一页
    info: [], // 请求的数据
    order_key:"",//money
    order_value:"asc",
    filterType:0,
    url:{
      0:'/manage/customer-list',
      3:'/manage/today-browse',
      2:'/manage/today-customer',
    },
  },
  // 点击筛选项
  clickFilter(e){
    let type = e.currentTarget.dataset.item;
    this.setData({
      filterType:type,
      pageNumber:1,
      info:[],
      hasNextPage:true,
    })
    this.getData();
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      filterType:options.type||0
    })
      this.getData();
  

  },
  // 获取数据
  getData(){
    var that = this;
    if(!this.data.url[this.data.filterType]) return;
    wx.request({
      url: app.globalData.urls + this.data.url[this.data.filterType],//'/manage/customer-list',
      method: 'POST',
      data: {
        "bdmin_user_id": util.getBdmin_user_id(),
        p: this.data.pageNumber,
        order_key:this.data.order_key,
        order_value:this.data.order_value,
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if (res.data.code == '200') {
          let data = res.data.data;
          let info = that.data.info.concat(data.list)
          info.forEach(val=>{
            if(val.money) val.money = val.money.toFixed(2)
          })
          that.setData({
            hasNextPage:data.totalPage>that.data.pageNumber,
            info:info
          })
        }
      }
    })
  },
  // 排序消费金额
  sortMoney(){
    let order_value = this.data.order_key =='money' && this.data.order_value=='asc' ? 'desc' : 'asc'
    this.setData({
      order_key:"money",
      pageNumber:1,
      info:[],
      hasNextPage:true,
      order_value:order_value,
    })
    this.getData()
  },
  // 排序订单数量
  sortNum(){
    let order_value = this.data.order_key =='num' && this.data.order_value=='asc' ? 'desc' : 'asc'
    this.setData({
      order_key:"num",
      order_value:order_value,
      pageNumber:1,
      info:[],
      hasNextPage:true,
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNumber:this.data.pageNumber+1
      })
      this.getData()
    }
  }
})